package com.cuiweiyou.nighttheme;

import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.cuiweiyou.nighttheme.ThemeLogic.ThemeChangeListener;

public class ThemeActivity extends Activity implements ThemeChangeListener,
		OnClickListener {

	private Button btn1, btn2;
	private ImageView mIIV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		switch(ThemeLogic.themeType){
		case 1:
			setTheme(R.style.day);
			break;
		case 2:
			setTheme(R.style.night);
		}
		
		setContentView(R.layout.activity_theme);

		btn1 = (Button) findViewById(R.id.day);
		btn1.setOnClickListener(this);
		btn2 = (Button) findViewById(R.id.night);
		btn2.setOnClickListener(this);
		mIIV = (ImageView) findViewById(R.id.iiv);

		ThemeLogic.getInstance().addListener(this);
	}

	// 按钮事件触发主题切换
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.day:
			ThemeLogic.themeType = 1; // 首先切换主题标识
			ThemeLogic.getInstance().notifiyChange(); // 然后执行监听器的切换功能
			break;
		case R.id.night:
			ThemeLogic.themeType = 2;
			ThemeLogic.getInstance().notifiyChange();
			break;
		}
	}

	@Override
	public void onThemeChanged() {
		switch (ThemeLogic.themeType) {
		
		/** 1。先更新主题 ！！！ */
		case 1:
			setTheme(R.style.day);
			break;
		case 2:
			setTheme(R.style.night);
			break;
		}
		
		/** 2.再提取属性 */
		TypedArray typedArray = obtainStyledAttributes(R.styleable.myStyle);
		// 3。最后更新控件
		btn1.setTextColor(typedArray.getColor(R.styleable.myStyle_mycolor,
				Color.YELLOW));
		btn2.setTextColor(typedArray.getColor(R.styleable.myStyle_mycolor,
				Color.YELLOW));
		mIIV.setImageDrawable(typedArray.getDrawable(R.styleable.myStyle_mypic));
		typedArray.recycle();
	}
}
